package com.cheng.nio;

import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * User: cheng Date: 2014/5/7 Time: 16:52
 */
public class FileChannelTest {

    public static void main(String[] args) throws Exception {
        RandomAccessFile raf = new RandomAccessFile("d:/nio.txt","rw");
        FileChannel fc = raf.getChannel();

        ByteBuffer bb = ByteBuffer.allocate(48);
        int bytesRead = fc.read(bb);

        while(bytesRead != -1){
            System.out.println("read:" + bytesRead);
            bb.flip();
            while(bb.hasRemaining()){
                System.out.println(bb.get());
            }
            bb.clear();
            bytesRead = fc.read(bb);
        }
        fc.close();
    }
}
